#!/usr/bin/env python
# -*- coding:utf-8 -*-
#encoding=utf-8
"""act.py."""
# Python's import is same as Java's import.
from logfmt import *
import time
import datetime
import conf
import comm
import inspect

class Act:
    """日志文本值类的公共父类
    """
    TAG = 0
    MIN_LEN = 2
    LOG_DAY = ""
    mod = None

    IAP_COUNTER = 0                          # iap的全局计数器，为了保证支付日志的唯一


    def _parse(self, linelist):
        pass

    def __init__(self, linelist):
        self.time = datetime.datetime.strptime("%s%s" % (Act.LOG_DAY, linelist[0]), "%Y-%m-%d%H%M%S")
        self._parse(linelist)


    @staticmethod
    def genParsedObj(line, pasday, log_split):
        Act.mod = inspect.getmodule(Act) if Act.mod is None else Act.mod
        if line <> None:
            line = line.strip()
            if line <> "":
                linelist = line.split(log_split)
                if len(linelist) <= Act.MIN_LEN:
                    return None
                Act.LOG_DAY = pasday.strftime("%Y-%m-%d")
                ### 对日志异常做兼容处理
                tag = 0
                try:
                    tag = int(linelist[1])
                except:
                    return None
                
                if tag == SDK_REG:
                    return  Reg(linelist)
                elif tag == SDK_LOGIN:
                    return  Login(linelist)
                elif tag == SDK_PAY:
                    return  Pay(linelist)
                else:
                    return None

"""
## 日志类
"""
class Reg(Act):
    """登录行为
    """
    
    TAG = SDK_REG

    def __init__(self, linelist):
        Act.__init__(self, linelist)
        pass

    def _parse(self, linelist):
        self.ip           = linelist[2]
        self.uid          = linelist[3]
        self.userName     = linelist[4]
        self.roleName     = linelist[5]
        self.userType     = int(linelist[6])
        llen              = len(linelist)
        self.phone        = linelist[7] if llen > 7 else ''
        self.model        = linelist[8] if llen > 8 else ''
        self.res          = linelist[9] if llen > 9 else ''
        self.lang         = linelist[10] if llen > 10 else ''
        self.brand        = linelist[11] if llen > 11 else ''


class Login(Act):
    """登录行为
    """
    TAG = SDK_LOGIN 
    
    def __init__(self, linelist):
        Act.__init__(self, linelist)

    def _parse(self, linelist):
        self.ip           = linelist[2]
        self.userId       = linelist[3]
        self.userName     = linelist[4]
        self.gameid       = linelist[5]
        self.appid        = linelist[6]
        self.os           = linelist[7]
        self.channel      = linelist[8]
        self.server       = linelist[9]
        self.userType     = 0
        
        
class Pay(Act):
    """支付行为
    """
    TAG = SDK_PAY
    
    def __init__(self, linelist):
        Act.__init__(self, linelist)
        pass

    def _parse(self, linelist):
        self.ip              = linelist[2]
        self.uid             = linelist[3]
        self.userName        = linelist[4]
        self.oid             = linelist[5]
        self.gameid          = linelist[6]
        self.appid           = linelist[7]
        self.os              = linelist[8]
        self.channel         = linelist[9]
        self.server          = linelist[10]
        self.cpid            = linelist[11]
        self.cpname          = linelist[12]
        self.price           = int(linelist[13])
        self.plistprice      = int(linelist[14])
        self.pnum            = int(linelist[15])
        self.appUserId       = linelist[16]
        self.appUserName     = linelist[17]
        self.allRMB          = int(linelist[18])
        self.item            = int(linelist[19])
        self.gameLevel       = int(linelist[20])
        self.gameVIPLevel    = int(linelist[21])
        
        
        
        
        

        
        